package com.ruoyi.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.domain.Student;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * @Author： yu.Tan
 * @Date: 2020/8/20 11:05
 * @Description:
 */
@Controller
public class SendController {

    @Autowired
    private RabbitTemplate template;

    @GetMapping("/sendDirect")
    private @ResponseBody
    String sendDirect(String message) throws Exception {
        Student user = new Student(UUID.randomUUID().toString(), message, "123456", "sendDirect");
        String jsonString = JSON.toJSONString(user);
        template.convertAndSend("DirectExchange-1027", "DirectRouting", jsonString.getBytes("UTF-8"));
        return "OK,sendDirect:" + message;
    }

    @GetMapping("/sendTopicFirst")
    private @ResponseBody
    String sendTopicFirst(String message) throws Exception {
        Student user = new Student(UUID.randomUUID().toString(), message, "123456", "sendTopicFirst");
        String jsonString = JSON.toJSONString(user);
        template.convertAndSend("topicExchange", "topic.first", jsonString.getBytes("UTF-8"));
        return "OK,sendTopicFirst:" + message;
    }

    @GetMapping("/sendTopicSecond")
    private @ResponseBody
    String sendTopicSecond(String message) throws Exception {
        Student user = new Student(UUID.randomUUID().toString(), message, "123456", "sendTopicSecond");
        String jsonString = JSON.toJSONString(user);
        template.convertAndSend("topicExchange", "topic.second", jsonString.getBytes("UTF-8"));
        return "OK,sendTopicSecond:" + message;
    }


    @GetMapping("/sendFanout")
    private @ResponseBody
    String sendFanout(String message) throws Exception {
        Student user = new Student(UUID.randomUUID().toString(), message, "123456", "sendFanout");
        String jsonString = JSON.toJSONString(user);
        template.convertAndSend("fanoutExchange", null, jsonString.getBytes("UTF-8"));
        return "OK,sendFanout:" + message;
    }
}
